package cn.maxxieyi.aichatbot.holder;

import cn.zhouyafeng.itchat4j.api.MessageTools;
import cn.zhouyafeng.itchat4j.beans.BaseMsg;
import cn.zhouyafeng.itchat4j.beans.RecommendInfo;
import cn.zhouyafeng.itchat4j.face.IMsgHandlerFace;
import lombok.extern.slf4j.Slf4j;

/**
 * 简单示例程序，收到文本信息自动回复原信息，收到图片、语音、小视频后根据路径自动保存
 */
@Slf4j
public class ItchatSimpleMsgHander implements IMsgHandlerFace {

    @Override
    public String textMsgHandle(BaseMsg msg) {
        // String docFilePath = "D:/itchat4j/pic/1.jpg"; // 这里是需要发送的文件的路径
        String text = msg.getText();
        if (!msg.isGroupMsg()) { // 是否群消息
            // String userId = msg.getString("FromUserName");
            // MessageTools.sendFileMsgByUserId(userId, docFilePath); // 发送文件
            // MessageTools.sendPicMsgByUserId(userId, docFilePath);
            this.log.info(text);
//            if (text.equals("111")) {
//                WechatTools.logout();
//            }
//            if (text.equals("222")) {
//                WechatTools.remarkNameByNickName("yaphone", "Hello");
//            }
//            if (text.equals("333")) { // 测试群列表
//                log.info(WechatTools.getGroupNickNameList());
//                System.out.print(WechatTools.getGroupIdList());
//                System.out.print(Core.getInstance().getGroupMemeberMap());
//            }

            return "收到";
        }else{
            log.info("groupMsg:{}",msg);
        }
        return null;
    }


    @Override
    public String picMsgHandle(BaseMsg msg) {
       /* String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());// 这里使用收到图片的时间作为文件名
        String picPath = "D://itchat4j/pic" + File.separator + fileName + ".jpg"; // 调用此方法来保存图片
        DownloadTools.getDownloadFn(msg, MsgTypeEnum.PIC.getType(), picPath); // 保存图片的路径*/
        this.log.info("收到图片消息");
        return "收到图片信息";
    }

    @Override
    public String voiceMsgHandle(BaseMsg msg) {
       /* String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
        String voicePath = "D://itchat4j/voice" + File.separator + fileName + ".mp3";
        DownloadTools.getDownloadFn(msg, MsgTypeEnum.VOICE.getType(), voicePath);*/
        this.log.info("收到声音消息");
        return "收到声音消息";
    }

    @Override
    public String viedoMsgHandle(BaseMsg msg) {
       /* String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
        String viedoPath = "D://itchat4j/viedo" + File.separator + fileName + ".mp4";
        DownloadTools.getDownloadFn(msg, MsgTypeEnum.VIEDO.getType(), viedoPath);*/
        this.log.info("收到视频消息");
        return "视频保存成功";
    }

    @Override
    public String nameCardMsgHandle(BaseMsg msg) {
        this.log.info("收到名片消息");
        return "收到名片消息";
    }

    @Override
    public void sysMsgHandle(BaseMsg msg) { // 收到系统消息
        String text = msg.getContent();
        this.log.info("收到系统消息:"+text);
    }

    @Override
    public String verifyAddFriendMsgHandle(BaseMsg msg) {
        MessageTools.addFriend(msg, true); // 同意好友请求，false为不接受好友请求
        RecommendInfo recommendInfo = msg.getRecommendInfo();
        String nickName = recommendInfo.getNickName();
        String province = recommendInfo.getProvince();
        String city = recommendInfo.getCity();
        String text = "你好，来自" + province + city + "的" + nickName + "， 欢迎添加我为好友！";
        return text;
    }

    @Override
    public String mediaMsgHandle(BaseMsg msg) {
      String fileName = msg.getFileName();
      /*    String filePath = "D://itchat4j/file" + File.separator + fileName; // 这里是需要保存收到的文件路径，文件可以是任何格式如PDF，WORD，EXCEL等。
        DownloadTools.getDownloadFn(msg, MsgTypeEnum.MEDIA.getType(), filePath);*/
        return "收到文件" + fileName ;
    }

}
